<?php
class MZip {
    
    public static function extractTo($src, $dest){
        if (extension_loaded('zip')){
            $zip = new ZipArchive();
            if ($zip->open($src) !== true){
                return false;
            }
            try {
                $zip->extractTo($dest);
            } catch (Exception $exc) {
                return false;
            }
            $zip->close();
            return true;
        } else if (extension_loaded('zlib')){
            if (($zip = zip_open($src)) === false){
                return false;
            }
            while ($entry = zip_read($zip)){
                if (zip_entry_open($zip, $entry)){
                    $name = zip_entry_name($entry);
                    $content = zip_entry_read($entry, zip_entry_filesize($entry));
                    zip_entry_close($entry);
                    if (strpos($name, '/') !== false){
                        MFile::createDirectoryRecursively($dest.DS.dirname($name));
                    }
                    $file = fopen($dest.DS.$name, 'w');
                    fwrite($file, $content);
                    fclose($file);
                } else {
                    zip_close($zip);
                    return false;
                }
            }
            zip_close($zip);
            return true;
        } else {
            // TODO : Throw MException
        }
    }
    
}
?>
